<template>
	<view class="height-screen">
		<cu-custom bgColor="bg-white" isBack>
			<block slot="content">{{$t('filed.pickerAdd')}}</block>
		</cu-custom>
		<u-radio-group v-model="radiovalue1">
			<view class="grid gap margin-ht" style="margin-top: 20rpx;width: 100%;">
				<view class="flex flex-direction padding-lr bg-white radius-sm" v-for="(item,index) in addressList"
					:key="index" @tap.stop="handleAddress(item)">
					<view class="padding-tb-lg u-border-bottom flex flex-direction">
						<view class="flex font-400 align-center text-black text-df">
							<text>{{item.name}}</text>
							<text class="margin-left-sm">{{item.phone}}</text>
						</view>
						<text class="margin-top font-400 text-gray text-df">{{item.city}}{{item.address}}</text>
					</view>
					<view class="flex justify-between padding-tb">
						<view class="flex align-center">
							<u-radio :name="item.id" activeColor="#06C168"
								@change="handleChange(item.id,item.default)"></u-radio>
							<text class="font-400 margin-left-xs"
								style="font-size: 26rpx;color: #555555;">{{$t('filed.isDefault')}}</text>
						</view>
						<view class="flex align-center">
							<view class="flex align-center" @tap.stop="handleDel(item.id,index)">
								<image src="@/static/img/addressDel.png" style="width: 23rpx;height: 26rpx;"></image>
								<text class="margin-left-sm text-gray text-sm font-400">{{$t('filed.del')}}</text>
							</view>
							<view class="flex align-center margin-left" @tap.stop="handleEdit(item.id)">
								<image src="@/static/img/addressEdit.png" style="width: 26rpx;height: 26rpx;"></image>
								<text class="margin-left-sm text-gray text-sm font-400">{{$t('filed.edit')}}</text>
							</view>
						</view>
					</view>
				</view>
				<section class="empty" v-if="!addressList.length">
					<u-empty mode="list"  :text="$t('list.empty')" />
				</section>
			</view>
		</u-radio-group>
		<u-gap height="100" bgColor="#f1f1f1"></u-gap>
		<view style="position: fixed;bottom: 60rpx;left: 25rpx;right: 25rpx;">
			<view class="setButton" style="height: 90rpx;background: #06C168;border-radius: 40rpx;"
				@tap.stop="helper.routerNav('/pages/mine/addAddress',{isEdit:0})">
				<text class="font-400 text-df text-white">{{$t('filed.addAddress')}}</text>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		getAddressList,
		addressDel,
		setDefault,
		getDefault
	} from '@/apis'
	export default {
		data() {
			return {
				radiovalue1: '',
				page: 1,
				addressList: [],
				isSelect: 0
			}
		},
		onLoad(options) {
			this.isSelect = options.isSelect
		},
		onShow() {
			this.getAddressList()
			this.getDefault()
		},
		methods: {
			handleAddress(item) {
				if (this.isSelect == 1) {
					uni.navigateBack({
						delta: 1,
						success: () => {
							uni.$emit('selectAddress', item)
						}
					})
				}
			},
			async getAddressList() {
				const res = await getAddressList({
					id: 1
				})
				this.addressList = res
			},
			async getDefault() {
				const res = await getDefault()
				this.radiovalue1 = res.id
			},
			async handleDel(id, index) {
				await addressDel({
					id
				})
				this.helper.showToast(this.$t('message.success'))
				this.addressList.splice(index, 1)
			},
			async handleEdit(id) {
				this.helper.routerNav('/pages/mine/addAddress', {
					isEdit: 1,
					id
				})
			},
			async handleChange(event, defaultAddress) {
				await setDefault({
					id: event,
					default: defaultAddress == 0 ? 1 : 0
				})
				this.helper.showToast(this.$t('message.success'))
			}
		}
	}
</script>

<style lang="scss" scoped>
</style>